﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FriendStar.Code;
using FriendStar.Application.MainData;
using FriendStar.Domain.Entity.MainData;
using FriendStar.Application.BOM;

namespace FriendStar.Web.Areas.MainData.Controllers
{
    public class CustomerInfoController : ControllerBase
    {
        //
        // GET: /MainData/CustomerInfo/
        V_BUS_SJ_DictionaryApp v_bus_sj_dictionaryApp = new V_BUS_SJ_DictionaryApp();
        V_ZSJ_A1_KH_CustomerInformationApp v_zsj_a1_kh_customerinformationApp = new V_ZSJ_A1_KH_CustomerInformationApp();
        ZSJ_A1_KH_CustomerInformationApp zsj_a1_kh_customerinformationApp = new ZSJ_A1_KH_CustomerInformationApp();

        [HttpGet]
        [HandlerAuthorize]
        public override ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult GetCustomerListJson(Pagination pagination, string UnitNO, string UnitFullName, string UnitShortName)
        {
            var data = new
            {
                rows = v_zsj_a1_kh_customerinformationApp.GetList(pagination, UnitNO, UnitFullName, UnitShortName),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetCustomerInfoSortJson(string sidx, string sord, int rows, string UnitNO, string UnitFullName, string UnitShortName, string currNumber)
        {
            Pagination pagination = new Pagination();
            pagination.rows = int.MaxValue;
            pagination.page = 1;
            pagination.sidx = sidx;
            pagination.sord = sord;
            var list = v_zsj_a1_kh_customerinformationApp.GetList(pagination, UnitNO, UnitFullName, UnitShortName);
            int index = 0;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].UnitID == currNumber)
                {
                    index = i + 1;
                    break;
                }
            }
            if (index > 0)
            {
                var data = new
                {
                    currIndex = index % rows,
                    currPage = (index % rows == 0) ? (index / rows) : (index / rows) + 1
                };
                return Content(data.ToJson());
            }
            else
            {
                var data = new
                {
                    currIndex = 1,
                    currPage = 1
                };
                return Content(data.ToJson());
            }
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult CustomerInput(string status, string number)
        {
            ViewBag.status = (status != "U") ? "I" : status;
            ViewBag.number = (number == null) ? "" : number;
            ViewBag.purchaseType = v_bus_sj_dictionaryApp.GetListByClassNo("11");
            ViewBag.creditLevel = v_bus_sj_dictionaryApp.GetListByClassNo("12");
            ViewBag.uniteNature = v_bus_sj_dictionaryApp.GetListByClassNo("13");
            ViewBag.productType = v_bus_sj_dictionaryApp.GetListByClassNo("01");
            ViewBag.currencyType = v_bus_sj_dictionaryApp.GetListByClassNo("14");
            if (status == "U")
            {
                ViewBag.oldEntity = zsj_a1_kh_customerinformationApp.GetForm(number).ToJson();
            }
            return View();
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitCustomerInfoForm(string status, ZSJ_A1_KH_CustomerInformationEntity entity, string keyValue)
        {
            if (status == "I")
            {
                if (zsj_a1_kh_customerinformationApp.CheckedEntity(entity))
                {
                    zsj_a1_kh_customerinformationApp.SubmitForm(entity, "");
                    return Success("新增操作成功。", entity.UnitID);
                }
                else
                {
                    return Error("新增失败，已经存在相同客户编号数据。");
                }
            }
            else if (status == "U")
            {
                if (zsj_a1_kh_customerinformationApp.CheckedEntity(entity))
                {
                    ZSJ_A1_KH_CustomerInformationEntity oldEntity = zsj_a1_kh_customerinformationApp.GetForm(entity.UnitID);
                    if (oldEntity != null)
                    {
                        zsj_a1_kh_customerinformationApp.SubmitForm(entity, entity.UnitID);
                        return Success("修改操作成功。", entity.UnitID);
                    }
                    else
                    {
                        return Error("修改失败，没找到待修改的客户数据。");
                    }
                }
                else
                {
                    return Error("修改失败，已经存在相同客户编号数据。");
                }
            }
            else 
                if (status == "D")
            {

                ZSJ_A1_KH_CustomerInformationEntity oldEntity = zsj_a1_kh_customerinformationApp.GetForm(keyValue);
                if (oldEntity != null)
                {
                    zsj_a1_kh_customerinformationApp.DeleteForm(oldEntity.UnitID);

                    return Success("删除操作成功。");
                }
                else
                {
                    return Error("删除失败，没找到待删除的托盘数据。");
                }
            }
            else
            {
                return Error("不可识别的操作类型。");
            }
        }
    }
}
